package com.zhihaoliang.animationtrain.animator

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.animation.ValueAnimator.AnimatorUpdateListener
import android.content.Context
import android.graphics.Canvas
import android.graphics.RadialGradient
import android.graphics.Shader
import android.graphics.drawable.ShapeDrawable
import android.graphics.drawable.shapes.OvalShape
import android.os.Bundle
import android.view.View
import android.view.animation.AccelerateInterpolator
import android.view.animation.DecelerateInterpolator
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import com.zhihaoliang.animationtrain.R
import com.zhihaoliang.animationtrain.annotation.Info
import com.zhihaoliang.animationtrain.util.ShapeHolder

private const val Y_POINT = 25F
@Info("动画的clone操作", "演示动画的clone的操作")
class AnimationCloningActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.animator_activity_animation_cloning)

        val container =
            findViewById<LinearLayout>(R.id.animator_containner)
        val animView = MyAnimationView(this)
        container.addView(animView)

        findViewById<View>(R.id.start_button).setOnClickListener { animView.startAnimation() }
    }

   private class MyAnimationView(context: Context) :
        View(context),
        AnimatorUpdateListener {
        private val balls =
            listOf(addBall(50f), addBall(150f), addBall(250f), addBall(350f))
        private var animation: AnimatorSet? = null

        private fun createAnimation() {
            if (animation == null) {
                val anim1 = ObjectAnimator.ofFloat(
                    balls[0], "y",
                    0f, height - balls[0].height
                ).setDuration(500)
                val anim2 = anim1.clone()
                anim2.target = balls[1]
                anim1.addUpdateListener(this)
                val ball2: ShapeHolder = balls[2]
                val animDown = ObjectAnimator.ofFloat(
                    ball2, "y",
                    0f, height - ball2.height
                ).setDuration(500)
                animDown.interpolator = AccelerateInterpolator()
                val animUp = ObjectAnimator.ofFloat(
                    ball2, "y",
                    height - ball2.height, 0f
                ).setDuration(500)
                animUp.interpolator = DecelerateInterpolator()
                val s1 = AnimatorSet()
                s1.playSequentially(animDown, animUp)
                animDown.addUpdateListener(this)
                animUp.addUpdateListener(this)
                val s2 = s1.clone().also {
                    it.setTarget(balls[3])
                }
                animation = AnimatorSet()
                animation!!.playTogether(anim1, anim2, s1)
                animation!!.playSequentially(s1, s2)
            }
        }

        private fun addBall(x: Float): ShapeHolder {
            val circle = OvalShape()
            circle.resize(
                50f * resources.displayMetrics.density,
                50f * resources.displayMetrics.density
            )
            val drawable = ShapeDrawable(circle)
            val shapeHolder = ShapeHolder(drawable)
            shapeHolder.width = 50f * resources.displayMetrics.density
            shapeHolder.height = 50f * resources.displayMetrics.density
            shapeHolder.x = x - 25f
            shapeHolder.y = Y_POINT - 25f
            val red = (100 + Math.random() * 155).toInt()
            val green = (100 + Math.random() * 155).toInt()
            val blue = (100 + Math.random() * 155).toInt()
            val color = -0x1000000 or (red shl 16) or (green shl 8) or blue
            val paint =
                drawable.paint //new Paint(Paint.ANTI_ALIAS_FLAG);
            val darkColor = -0x1000000 or (red / 4 shl 16) or (green / 4 shl 8) or blue / 4
            val gradient = RadialGradient(
                37.5f, 12.5f,
                50f, color, darkColor, Shader.TileMode.CLAMP
            )
            paint.shader = gradient
            shapeHolder.paint = paint
            return shapeHolder
        }

        override fun onDraw(canvas: Canvas) {
            for (ball in balls) {
                canvas.save()
                canvas.translate(ball.x, ball.y)
                ball.shapeDrawable.draw(canvas)
                canvas.restore()
            }
        }

        fun startAnimation() {
            createAnimation()
            animation!!.start()
        }

        override fun onAnimationUpdate(animation: ValueAnimator) {
            invalidate()
        }


    }
}
